from django.conf import settings
import requests
from django.core.cache import cache

class XmCloudBaseClient():
    XU_API_URL = settings.XU_API_URL
    base_url = f"{XU_API_URL}/xu-cloud"
    # base_url = "http://10.8.0.6:8080/xu-cloud"
    corp_name = ""
    xm_token = ""
    hzfw_org_id = ""
    
    def request_api(self,api_url,method,data=None):
        request_url = f"{self.base_url}{api_url}"
        # xm_token = self.xm_token if (self.xm_token != None and self.xm_token != "") else cache.get(f"yida_xm_token:{self.corp_name}")
        headers = {
            'Authorization': self.xm_token,
            'Content-Type': 'application/json'
        }
        if method == "GET":
            response = requests.get(request_url,params=data, headers=headers)
        elif method == "POST":
            response = requests.post(request_url, json=data, headers=headers)
        if response.status_code == 200:
            return response.json()  # 返回JSON解析后的数据
        else:
            raise ValueError(f"请求失败，状态码：{response.status_code}")
    def create_model(self,model_info):
        '''创建模版'''
        api_url = "/detection-model/"
        return self.request_api(api_url,"POST",model_info)
        
    def gen_detection_record_url(self,render_id):
        '''
            获取渲染记录的url
        '''
        api_url = f"/detection-model-render-record/{render_id}/gen-url/"
        return self.request_api(api_url,"GET")

    def render_model_xlsx(self,model_id,render_data):
        '''
            excel模版渲染
        '''
        api_url = f"/detection-model/{model_id}/render-model-xlsx/"
        return self.request_api(api_url,"POST",render_data)
    def render_model(self,model_id,render_data):
        '''
            模版渲染
        '''
        api_url = f"/detection-model/{model_id}/render-model-docx/"
        return self.request_api(api_url,"POST",render_data)
    
    def post_office_file(self,file_info):
        '''
            模版渲染
        '''
        api_url = f"/wopi/files"
        return self.request_api(api_url,"POST",file_info)
    
    def sign_to_pdf_file(self,model_id,sign_data_parms):
        '''
            签名
        '''
        api_url = f"/detection-model/{model_id}/sign-to-area/"
        return self.request_api(api_url,"POST",sign_data_parms)
    
    def get_render_model_info(self,model_id):
        '''
            获取模版信息
        '''
        api_url = f"/detection-model/{model_id}/"
        return self.request_api(api_url,"GET")
    
    def create_project(self,data):
        '''
            创建工程
        '''
        api_url = f"/project/"
        return self.request_api(api_url,"POST",data)
    
    def get_detection_model_selection_list(self,name:str):
        '''
            获取检测模型列表
        '''
        api_url = f"/detection-model/select_options/"
        return self.request_api(api_url,"GET",{
            "name__icontains":name
        })
    
    def get_qualification_selection_list(self):
        '''获取资质列表'''
        api_url = f"/config/qualification/select_options/"
        return self.request_api(api_url,"GET")
    
    def get_exam_category_selection_list(self,qualification_id:str):
        '''
            获取检测大类列表
        '''
        api_url = f"/config/examine-category/select_options/"
        return self.request_api(api_url,"GET",{
            "qualification_id":qualification_id
        })
    
    def get_xm_exam_item_detail(self,id):
        '''
            获取检测项目详情
        '''
        api_url = f"/detection-project/{id}/"
        return self.request_api(api_url,"GET")
    
    def get_exam_item_selection_list(self,exam_category_id=None):
        '''
            获取检测项目列表
        '''
        api_url = f"/detection-project/select_options/"
        params = {
            "exam_category_id":exam_category_id,
        }
        return self.request_api(api_url,"GET",params)
    
    def get_exam_params_list(self,project_id):
        '''
            获取检测参数列表
        '''
        api_url = f"/detection-parameter/"
        params = {
            "project_id":project_id,
        }
        return self.request_api(api_url,"GET",params)
    
    def create_special_report_record(self,model_ids:list,name:str,pub_render_data:dict,sample_ids:list=[]):
        '''
            创建专项报告记录
        '''
        api_url = f"/special-report-record/"
        return self.request_api(api_url,"POST",{
            "name":name,
            "model_ids":model_ids,
            "pub_render_data":pub_render_data,
            "sample_ids":sample_ids
        })

    def get_model_info(self,model_id):
        '''
            获取模型信息
        '''
        api_url = f"/detection-model/{model_id}/"
        return self.request_api(api_url,"GET")
    
    def calc_cost(self,data):
        '''计算本组费用'''
        api_url = f"/detection-project/calc_xm_cost/"
        return self.request_api(api_url,"POST",data)
    
    def get_extra_params(self,id):
        '''获取检测项目额外自定义配置项'''
        api_url = f"/detection-project/{id}/extra_params/"
        return self.request_api(api_url,"GET")
    
    def create_cutout_task(self,data):
        '''创建视频截取任务'''
        api_url = f"/supervise-record/create-cutout-task/"
        return self.request_api(api_url,"POST", data)